<?php

namespace app\api\Logic\Campusclass;

use app\api\model\CampusClassTeacherSettingModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;

//教师一对一约课规则接口
class CampusClassTeacherSettingLogic
{

    /**
     * 判断当前日期老师是否可以约课
     * @param $teacherId
     * @param $date
     * @param array $ruleList //如果是日期段查询，建议传$ruleList，如果是单个日期筛选，可以不传
     * @return false
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function isCan($teacherId,$date,$ruleList = []){
        if(empty($ruleList)){
            $ruleList = $this->rule($teacherId);
        }

        //未设置约课规则，不可约课
        if(empty($ruleList)){
            return false;
        }

        $date = date('Y-m-d',strtotime($date));
        $week = date('w',strtotime($date));

        //符合当前日期相关的规则
        $trueRule = [];
        foreach ($ruleList as $val){
            //找到符合当前星期的约课规则
            if($val['week'] == $week && $val['style'] == 1){
                $trueRule[1][] = $val;
            }
            //找到符合该日期的约课规则
            if(strtotime($val['date']) == strtotime($date) && $val['style'] == 2){
                $trueRule[2][] = $val;
            }
        }

        $isCan = false;
        //存在当前星期的约课时间，则可约
        if(isset($trueRule[1]) && !empty($trueRule[1])){
            $isCan = true;
        }
        //由于特定日期规则优先级更高，因此在约课星期满足的情况下，还需判断特定日期是否允许约课
        if(isset($trueRule[2]) && !empty($trueRule[2])){
            foreach ($trueRule[2] as $val){
                //如果是全天休息，则不可约
                if($val['type'] == 1){
                    $isCan = false;
                    break;
                }else{
                    //否则都为特定时间可约
                    $isCan = true;
                }
            }
        }

        return $isCan;
    }

    /**
     * 教师约课规则
     * @param $teacherId
     * @param null $style
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function rule($teacherId,$style = null){
        $list = CampusClassTeacherSettingModel::where('teacher_id',$teacherId);

        //根据类型筛选
        if($style){
            $list = $list->where('style',$style);
        }

        $list = $list->field('style,week,date,type,class_time_start,class_time_end')
            ->order('class_time_start','asc')
            ->select();
        return objToArray($list) ?? [];
    }

    /**
     * 根据日期获取教师可约课时间
     * @param $teacherId
     * @param $date
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getTimeSlot($teacherId,$date){
        //获取教师约课规则
        $ruleList = $this->rule($teacherId);

        //教师当天可约课时间段
        $res = [];
        $date = date('Y-m-d',strtotime($date));
        $week = date('w',strtotime($date));

        //符合当前日期相关的规则
        $trueRule = [];
        foreach ($ruleList as $val){
            //找到符合当前星期的约课规则
            if($val['week'] == $week && $val['style'] == 1){
                $trueRule[1][] = $val;
            }
            //找到符合该日期的约课规则
            if(strtotime($val['date']) == strtotime($date) && $val['style'] == 2){
                $trueRule[2][] = $val;
            }
        }

        //由于特定日期规则优先级更高，因此先判断指定日期，若指定日期不存在，则判断约课规则
        if(isset($trueRule[2]) && !empty($trueRule[2])){
            foreach ($trueRule[2] as $key => $val){
                //如果是全天休息，则不可约
                if($val['type'] == 1){
                    $res = [];
                    break;
                }else{
                    $res[$key]['start_time'] = $val['class_time_start'];
                    $res[$key]['end_time'] = $val['class_time_end'];
                }
            }
        }else{
            //存在当前星期的约课时间，则可约
            if(isset($trueRule[1]) && !empty($trueRule[1])){
                foreach ($trueRule[1] as $key => $val){
                    $res[$key]['start_time'] = $val['class_time_start'];
                    $res[$key]['end_time'] = $val['class_time_end'];
                }
            }
        }

        return $res;
    }
}
